package com.easypan.controller;

import java.io.File;
import java.util.List;

import com.easypan.annotation.GlobalInterceptor;
import com.easypan.annotation.VerifyParam;
import com.easypan.entity.dto.SessionWebUserDto;
import com.easypan.entity.dto.UploadResultDto;
import com.easypan.entity.enums.FileCategoryEnums;
import com.easypan.entity.enums.FileDelFlagEnums;
import com.easypan.entity.enums.FileFolderTypeEnums;
import com.easypan.entity.po.FileInfo;
import com.easypan.entity.query.FileInfoQuery;
import com.easypan.entity.vo.FileInfoVO;
import com.easypan.entity.vo.PaginationResultVO;
import com.easypan.entity.vo.ResponseVO;
import com.easypan.service.FileInfoService;
import com.easypan.utils.CopyTools;
import com.easypan.utils.StringTools;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.text.html.HTML;

/**
 * 文件信息表
 Controller
 */
@RestController("fileInfoController")
@RequestMapping("/file")
public class FileInfoController extends CommonFileController{

	@Resource
	private FileInfoService fileInfoService;
	/**
	 * 根据条件分页查询
	 */
	@RequestMapping("/loadDataList")
	public ResponseVO loadDataList(HttpSession session,FileInfoQuery query, String category){

		FileCategoryEnums categoryEnum=FileCategoryEnums.getByCode(category);

		if(null!=categoryEnum){
			query.setFileCategory(categoryEnum.getCategory());
		}

		query.setUserId(getUserInfoFromSession(session).getUserId());
		query.setOrderBy("last_update_time desc");
		query.setDelFlag(FileDelFlagEnums.USING.getFlag());
		PaginationResultVO result=fileInfoService.findListByPage(query);
		return getSuccessResponseVO(convert2PaginationVO(result, FileInfoVO.class));
	}

	@RequestMapping("/uploadFile")
	@GlobalInterceptor(checkParams = true)
	public  ResponseVO uploadFile(HttpSession session,
								  String fileId,
								  MultipartFile file,
								  @VerifyParam(required = true) String fileName,
								  @VerifyParam(required = true) String filePid,
								  @VerifyParam(required = true) String fileMd5,
								  @VerifyParam(required = true) Integer chunkIndex,
								  @VerifyParam(required = true) Integer chunks) {

		SessionWebUserDto webUserDto = getUserInfoFromSession(session);
		UploadResultDto resultDto = fileInfoService.uploadFile(webUserDto, fileId, file, fileName, filePid, fileMd5, chunkIndex, chunks);
		return getSuccessResponseVO(resultDto);
	}

	@RequestMapping("/getImage/{imageFolder}/{imageName}")
	public void getImage(HttpServletResponse response, @PathVariable("imageName")String imageName, @PathVariable("imageFolder") String imageFolder){
		super.getImage(response,imageName,imageFolder);

	}

	@RequestMapping("/ts/getVideoInfo/{fileId}")
	public void getVideoInfo(HttpServletResponse response, HttpSession session, @PathVariable("fileId") @VerifyParam(required = true) String fileId) {
		SessionWebUserDto webUserDto = getUserInfoFromSession(session);
		super.getFile(response, fileId, webUserDto.getUserId());
	}

	@RequestMapping("/getFile/{fileId}")
	public void getFile(HttpServletResponse response, HttpSession session, @PathVariable("fileId") @VerifyParam(required = true) String fileId) {
		SessionWebUserDto webUserDto = getUserInfoFromSession(session);
		super.getFile(response, fileId, webUserDto.getUserId());
	}

	@RequestMapping("/newFoloder")
	@GlobalInterceptor(checkParams = true)
	public ResponseVO newFolder(
			HttpSession session,
			@VerifyParam(required = true)String filePid,
			@VerifyParam(required = true)  String fileName){
		SessionWebUserDto webUserDto=getUserInfoFromSession(session);
		FileInfo fileInfo=fileInfoService.newFolder(filePid,webUserDto.getUserId(),fileName);
		return getSuccessResponseVO(fileInfo);
	}

	@RequestMapping("/getFolderInfo")
	@GlobalInterceptor(checkParams = true)
	public ResponseVO getFolderInfo(HttpSession session,@VerifyParam (required = true) String path){
		SessionWebUserDto webUserDto=getUserInfoFromSession(session);
		return super.getFolderInfo(path,webUserDto.getUserId());
	}

	@RequestMapping("/rename")
	@GlobalInterceptor(checkParams = true)
	public ResponseVO rename(HttpSession session,
							 @VerifyParam(required = true) String fileId,
							 @VerifyParam(required = true) String fileName){
		SessionWebUserDto webUserDto=getUserInfoFromSession(session);
		FileInfo fileInfo=fileInfoService.rename(fileId,webUserDto.getUserId(),fileName);
		return getSuccessResponseVO(CopyTools.copy(fileInfo,FileInfo.class));
	}

	@RequestMapping("/loadAllFolder")
	@GlobalInterceptor(checkParams = true)
	public ResponseVO loadAllFolder(HttpSession session,@VerifyParam(required = true)String filePid,String currentFileIds){
		FileInfoQuery filequery=new FileInfoQuery();
		filequery.setUserId(getUserInfoFromSession(session).getUserId());
		filequery.setFilePid(filePid);
		filequery.setFolderType(FileFolderTypeEnums.FOLDER.getType());
		if(!StringTools.isEmpty(currentFileIds)){
			filequery.setExcludeFileIdArray(currentFileIds.split(","));
		}
		filequery.setDelFlag(FileDelFlagEnums.USING.getFlag());
		filequery.setOrderBy("create_time desc");
		List<FileInfo> fileInfoList=fileInfoService.findListByParam(filequery);
		return getSuccessResponseVO(CopyTools.copyList(fileInfoList,FileInfoVO.class));
	}

	@RequestMapping("/changeFileFolder")
	@GlobalInterceptor(checkParams = true)
	public ResponseVO changeFileFolder(HttpSession session,@VerifyParam (required = true) String fileIds,@VerifyParam (required = true)String filePid){
		SessionWebUserDto webUserDto=getUserInfoFromSession(session);
		fileInfoService.changeFileFolder(fileIds, filePid, webUserDto.getUserId());
		return getSuccessResponseVO(null);
	}

	@RequestMapping("/createDownloadUrl/{fileId}")
	@GlobalInterceptor(checkParams = true)
	public ResponseVO createDownloadUrl(HttpSession session,@PathVariable("fileId") @VerifyParam(required = true) String fileId){
		return super.createDownloadUrl(fileId,getUserInfoFromSession(session).getUserId());
	}

	@RequestMapping("/download/{code}")
	@GlobalInterceptor(checkParams = true)
	public void download(HttpServletRequest request, HttpServletResponse response, @PathVariable("code") @VerifyParam(required = true) String code) throws Exception {
		super.download(request, response, code);
	}

	@RequestMapping("/delFile")
	@GlobalInterceptor(checkParams = true)
	public ResponseVO delFile(HttpSession session, @VerifyParam(required = true) String fileIds){
		SessionWebUserDto webUserDto=getUserInfoFromSession(session);
		fileInfoService.removeFile2RecycleBatch(webUserDto.getUserId(),fileIds);
		return getSuccessResponseVO(null);


	}








}